package com.an.blog.third.mq.consumer;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.an.blog.admin.domain.BlogArticle;
import com.an.blog.third.es.domain.EsBlogArticle;
import com.an.blog.third.es.esmapper.EsBlogArticleMapper;
import com.an.blog.utils.BlogUniversalUtil;
import com.an.common.constant.MqConstants;
import com.an.system.service.ISysOssService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Collection;

/**
 * 博客 MQ 消费者
 * TODO 消费失败处理，后期完成
 *
 * @author Ann
 * @date 2024/06/12
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class BlogEsMqConsumer {


    private final ISysOssService ossService;

    private final EsBlogArticleMapper esBlogArticleMapper;

    /**
     * 处理发送插入文章2 ES消息
     *
     * @param blogArticle 博客文章
     * @author Ann
     * @date 2024/06/12
     */
    @RabbitListener(bindings = @QueueBinding(
        value = @Queue(value = MqConstants.ARTICLE_INSERT_QUEUE, durable = "true"),
        exchange = @Exchange(value = MqConstants.ARTICLE_EXCHANGE, type = "topic"),
        key = {MqConstants.ARTICLE_INSERT_KEY}
    ))
    public void handleSendInsertArticle2EsMessage(BlogArticle blogArticle) {
        CopyOptions copyOptions = CopyOptions.create().setIgnoreNullValue(true).setIgnoreProperties("articleImage", "isLiked",
            "articleTags", "categoryId");
        EsBlogArticle esBlogArticle = BeanUtil.toBean(blogArticle, EsBlogArticle.class, copyOptions);
        esBlogArticle.setArticleImage(ossService.getById(blogArticle.getArticleImage()).getUrl());
        BlogUniversalUtil.setArticleWebParameters(esBlogArticle, blogArticle.getCategoryId(),
            blogArticle.getArticleType(), blogArticle.getArticleId(), blogArticle.getCreateBy(),
            EsBlogArticle::setCategoryName, EsBlogArticle::setArticleType, EsBlogArticle::setAvatar,
            EsBlogArticle::setCreateBy, EsBlogArticle::setAuthorId, EsBlogArticle::setTags);
        Integer inserted = esBlogArticleMapper.insert(esBlogArticle);
        if (inserted > 0) {
            log.info("===博客文章插入ES消费成功===");
        }
    }

    /**
     * 处理从 ES 消息中发送删除文章
     *
     * @param articleIds 文章 ID
     * @author Ann
     * @date 2024/06/29
     */
    @RabbitListener(bindings = @QueueBinding(
        value = @Queue(value = MqConstants.ARTICLE_DELETE_QUEUE, durable = "true"),
        exchange = @Exchange(value = MqConstants.ARTICLE_EXCHANGE, type = "topic"),
        key = {MqConstants.ARTICLE_DELETE_KEY}
    ))
    public void handleSendRemoveArticleFromEsMessage(Collection<Long> articleIds) {
        Integer deleted = esBlogArticleMapper.deleteBatchIds(articleIds);
        if (deleted > 0) {
            log.info("===博客文章从ES删除消费成功===");
        }
    }

    /**
     * 处理从 ES 消息中发送删除文章
     *
     * @param article 文章
     * @author Ann
     * @date 2024/06/29
     */
    @RabbitListener(bindings = @QueueBinding(
        value = @Queue(value = MqConstants.ARTICLE_UPDATE_QUEUE, durable = "true"),
        exchange = @Exchange(value = MqConstants.ARTICLE_EXCHANGE, type = "topic"),
        key = {MqConstants.ARTICLE_UPDATE_KEY}
    ))
    public void handleSendUpdateArticle2EsMessage(BlogArticle article) {
        EsBlogArticle esBlogArticle = BeanUtil.toBean(article, EsBlogArticle.class);
        Integer updated = esBlogArticleMapper.updateById(esBlogArticle);
        if (updated > 0) {
            log.info("===ES博客文章更新消费费成功===");
        }
    }
}
